<template>
    <div>
        <div class="permissTitle">
            <el-input size="small" placeholder="Please Input The English Of Role" v-model="role.name">
                <template slot="prepend">ROLE_</template>
            </el-input>
            <el-input size="small" v-model="role.roleName" @keydown.enter.native="addRole" placeholder="请输入角色中文"></el-input>
            <el-button size="small" icon="el-icon-circle-plus" style="background-color: rgba(168,145,205,0.76)" @click="addRole" round>
                添加角色
            </el-button>
        </div>
        <div class="permissMain">
            <el-collapse v-model="activeNames" accordion @change="change">
                <el-collapse-item :title="r.roleName" :name="r.id" v-for="(r,index) in roles" :key="index">
                    <template slot="title">
                        <i class="header-icon el-icon-info">&nbsp;&nbsp;&nbsp;&nbsp;{{r.roleName}}</i>
                    </template>
                    <el-card class="box-card" shadow="hover">
                        <div slot="header" class="clearfix">
                            <span>可访问资源</span>
                            <el-input
                                    style="width: 500px;margin-left: 20px"
                                    prefix-icon="el-icon-share"
                                    placeholder="输入关键字进行过滤">
                            </el-input>
                            <el-button size="medium" style="float: right; padding: 3px 0;color: #ff91cc"
                                       icon="el-icon-delete" @click="deleteRole(r)" type="text"></el-button>
                        </div>
                        <div>
                            <el-tree
                                    :data="allMenus"
                                    :props="props"
                                    ref="tree"
                                    :key="index"
                                    show-checkbox
                                    :default-checked-keys="selectedMenus"
                                    node-key="id">
                            </el-tree>
                            <div style="display: flex;justify-content: flex-end;margin-top: 8px;margin-bottom: -12px">
                                <el-button size="mini" icon="el-icon-circle-close"
                                           style="background-color: rgba(239,0,0,0.43)" @click="cancel" round>取消
                                </el-button>
                                <el-button size="mini" icon="el-icon-circle-check"
                                           style="background-color: rgba(0,124,0,0.5)" @click="toUpdate(r.id,index)"
                                           round>确认
                                </el-button>
                            </div>

                        </div>
                    </el-card>
                </el-collapse-item>
            </el-collapse>
        </div>
    </div>
</template>

<script>
    export default {
        name: "PermissMana",
        data() {
            return {
                role: {
                    name: '',
                    roleName: ''
                },
                roles: [],
                allMenus: [],
                props: {
                    children: 'children',
                    label: 'name'
                },
                selectedMenus: [],
                activeNames: -1
            }
        },
        mounted() {
            this.initRoles();
        },
        methods: {
            addRole(){
              if(this.role.name && this.role.roleName){
                this.postRequest('/system/basic/permission/',this.role).then(resp=>{
                    if(resp){
                        this.initRoles();
                        this.role.name='';
                        this.role.roleName='';
                    }
                })
              }else {
                  this.$message.error('🐎の，两个都不能为空！━┳━　━┳━')
              }
            },
            deleteRole(role){
                this.$confirm('此操作将永久删除[' + role.roleName + ']角色, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleteRequest('/system/basic/permission/role/' + role.id).then(resp => {
                        if (resp) {
                            this.initRoles();
                        }
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            cancel(){
                this.activeNames = -1;
            },
            toUpdate(rid,index) {
                let tree = this.$refs.tree[index];
                let selectedKeys = tree.getCheckedKeys(true);
                let url = '/system/basic/permission/?rid=' + rid;
                selectedKeys.forEach(key => {
                    url += '&mids=' + key;
                });
                this.putRequest(url).then(resp => {
                    if (resp) {
                        this.activeNames = -1;
                    }
                })
            },
            change(rid) {
                if (rid) {
                    this.initAllMenus();
                    this.initSelectMenus(rid);
                }
            },
            initAllMenus() {
                this.getRequest('/system/basic/permission/menus').then(resp => {
                    if (resp) {
                        this.allMenus = resp;
                    }
                })
            },
            initSelectMenus(rid) {
                this.getRequest('/system/basic/permission/mid/' + rid).then(resp => {
                    if (resp) {
                        this.selectedMenus = resp;
                    }
                })
            },
            initRoles() {
                this.getRequest('/system/basic/permission/').then(resp => {
                    if (resp) {
                        this.roles = resp;
                    }
                })
            }
        }
    }
</script>

<style>

    .permissTitle {
        display: flex;
        justify-content: flex-start;
    }

    .permissTitle .el-input {
        width: 400px;
        margin-right: 15px;
    }

    .permissMain {
        margin-top: 15px;
        width: 100%;
    }
</style>